package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;

/**
 * 资源规则相关接口
 * 
 * @author dongkai
 *
 */
@RestController
@RequestMapping("/res/rule")
public class ResourceRuleController {

	private final String BASE_SERVICE = "/res/rule";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 获取关联关系
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/relations", method = { RequestMethod.POST })
	public BsmResult relations(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONObject.parseObject(params);
		if (null != jsonObject) {
			Map<String, Object> paramMap = MapTools.simpleMap("category", jsonObject.getString("category"));
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/relations", BoCloudMethod.OTHERS,
					null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 获取类型
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/category", method = { RequestMethod.POST })
	public BsmResult category(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONObject.parseObject(params);
		if (null != jsonObject) {
			Map<String, Object> paramMap = MapTools.simpleMap("relation", jsonObject.getString("relation"));
			paramMap.put("category", jsonObject.getString("category"));
			RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/category", BoCloudMethod.OTHERS,
					null, paramMap, request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

}
